<!-- 封装的弹出框组件 -->
<template>
  <div class="popup" v-if="popupVisible">
    <!-- 弹出框的背景 -->
    <Transition name="fade">
      <div
        class="popup-bg"
        @click.stop.prevent="hide"
        v-show="popupVisible"
      ></div>
    </Transition>

    <Transition name="popup-slide-up">
      <div class="popup-wrapper" v-show="visible">
        <!-- 弹出框的标题 -->
        <div class="popup-title" v-if="title && title.length > 0">
          {{ title }}
        </div>

        <!-- 弹出框的按钮 -->
        <div
          class="popup-btn"
          :class="{ danger: item.type === 'danger' }"
          v-for="(item, index) in btn"
          :key="index"
          @click="item.click"
        >
          {{ item.text }}
        </div>
      </div>
    </Transition>
  </div>
</template>

<script>
export default {
  name: 'popup',
  props: {
    title: String,
    btn: Array, // 适配多按钮情况
  },
  data() {
    return {
      popupVisible: false, // 控制背景的显示与隐藏
      visible: false, // 控制弹出框界面的显示与隐藏
    };
  },
  methods: {
    show() {
      this.popupVisible = true;
      // 通过宏任务队列解决页面之间显示,动画无法正常执行的问题
      setTimeout(() => {
        this.visible = true;
      });
    },
    hide() {
      // 通过两个变量来控制,先隐藏弹出框,再隐藏按钮,解决页面直接隐藏导致动画无法执行的问题
      this.visible = false;
      setTimeout(() => {
        this.popupVisible = false;
      }, 200);
    },
  },
};
</script>

<style scoped lang="scss">
@import '../../assets/styles/global';
.popup {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 2000;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.4);

  .popup-bg {
    width: 100%;
    height: 100%;
  }

  .popup-wrapper {
    position: fixed;
    left: 0;
    bottom: 0;
    z-index: 2000;
    width: 100%;
    background-color: #fff;

    .popup-title {
      width: 100%;
      height: px2rem(44);
      border-bottom: px2rem(1) solid #eee;
      font-size: px2rem(12);
      line-height: px2rem(14);
      padding: px2rem(15);
      box-sizing: border-box;
      color: #999;
      @include center;
    }

    .popup-btn {
      width: 100%;
      height: px2rem(60);
      border-bottom: px2rem(1) solid #eee;
      font-size: px2rem(16);
      color: #666;
      font-weight: bold;
      @include center;
      &.danger {
        color: $color-pink;
      }
    }
  }
}
</style>
